<!DOCTYPE HTML>
<html lang="cn" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <%include("/common/header.html",{title:''}){}%>
    <title>商品列表</title>
    <link rel="stylesheet" href="css/formSelects-v4.css"/>
</head>
<body>

<div class="x-nav" style="margin: 20px auto auto 15px;">
      <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
        <a href="javascript:;">首页</a><span lay-separator="">/</span>
        <a href="javascript:;">订单管理</a><span lay-separator="">/</span>
        <a><cite>增值服务订单</cite></a>
      </span>
    <a class="layui-btn layui-btn-primary layui-btn-small" style="margin: -5px 14px 0px 0px;float:right;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh-3"></i>
    </a>
</div>

<div class="layui-form layui-card-header layuiadmin-card-header-auto">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">审核状态</label>
            <div class="layui-input-block">
                <select type="text" name="status" id="status" autocomplete="off" class="layui-select">
                    <option value="">-请选择-</option>
                    <option value="0">已通过</option>
                    <option value="1">财务审核中</option>
                    <option value="2">管理审核中</option>
                    <option value="3">未通过</option>
                </select>
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
        </div>
    </div>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
            <script type="text/html" id="toolbarDemo">
                <button class="layui-btn layuiadmin-btn-useradmin" lay-event="add">新增订单</button>
                <!--<button class="layui-btn layuiadmin-btn-useradmin" lay-event="combination">组合商品</button>-->
            </script>

            <script type="text/html" id="table-view-a">
                <a class="layui-btn  layui-btn-xs" lay-event="hutan"><i class="layui-icon layui-icon-form"></i>查看</a>
            </script>
            <script type="text/html" id="imgTpl">
                <img style="display: inline-block; width: 50%; height: 100%;" src={{ d.avatar }}>
            </script>
            <script type="text/html" id="table-useradmin-webuser">
                {{#  if(d.status === 1&&d.deletetype===0){ }}
                <div style="color: #0e94ea">财务审核中</div>
                {{#  } }}
                {{#  if(d.status === 2&&d.deletetype===0){ }}
                <div style="color: #0e94ea">管理审核</div>
                {{#  } }}
                {{#  if(d.status === 0&&d.deletetype===0){ }}
                <div style="color: #0e94ea">通过</div>
                {{#  } }}
                {{#  if(d.deletetype===1){ }}
                <div style="color: red">已经撤销</div>
                {{#  } }}
                {{#  if(d.status===3){ }}
                <div style="color: red">已经驳回</div>
                {{#  } }}
            </script>
            <script type="text/html" id="table-useradmin-opt">
                {{#  if(d.status === 1 && d.deletetype==0){ }}
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="chexiao"><i
                        class="layui-icon layui-icon-refresh-3"></i>撤销订单</a>
                {{#  }else if(d.status === 3  && d.deletetype==0){ }}
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="resbon"><i
                        class="layui-icon layui-icon-close-fill"></i>驳回原因</a>
                {{#  }else{ }}
                <a class="layui-btn layui-btn-disabled layui-btn-xs" lay-event="null"><i
                        class="layui-icon layui-icon-close-fill"></i>不可操作</a>
                {{#  } }}

            </script>
        </div>
    </div>
</div>

<div class="layui-form" lay-filter="layuiadmin-form-useradmins"
     id="layuiadmin-form-useradmins"
     style="padding: 15px;display: none">
    <form class="layui-form" action="">
        <div class="layui-form-item" style="width: 100%">
            <label class="layui-form-label">选择用户</label>
            <div class="layui-input-block">
                <select name="userid" id="userid" lay-search="">

                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">增值服务</label>
            <div class="layui-input-block" id="listlist">

            </div>
        </div>
        <!--

            <div class="layui-form-item">
              <div class="layui-inline">
                <label class="layui-form-label">优惠券</label>
                <div class="layui-input-inline">
                  <select name="quiz">
                    <option value="">请选择问题</option>
                    <option value="你工作的第一个城市">优惠券</option>
                    <option value="你的工号">代金券</option>
                  </select>
                </div>
              </div>

            </div>
        -->


        <!-- <div class="layui-form-item" pane="">
           <label class="layui-form-label">增值服务</label>
           <div class="layui-input-block">
             <input type="checkbox" name="like1[write]" lay-skin="primary" title="全局展示" checked="">
             <input type="checkbox" name="like1[read]" lay-skin="primary" title="人工智能">
           </div>
         </div>-->


        <div class="layui-form-item" style="width: 100%">
            <label class="layui-form-label">价格</label>
            <div class="layui-input-block">
                <input type="text" id="totalPrice" name="totalPrice" value="0" readonly lay-verify="required" placeholder="￥"
                       autocomplete="off" class="layui-input">
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="component-form-element">立即提交</button>
            </div>
        </div>
    </form>
</div>
<div class="layui-form" lay-filter="table-huyang"
     id="table-huyang"
     style="padding: 15px;display: none">
    <form class="layui-form" action="" lay-filter="component-form-element-b" id="myform1">
        <div class="layui-form-item">
            <h3 class="layui-form-label">商品信息</h3>
            <table class="layui-table" lay-size="lg">

                <thead>
                <tr>
                    <th width="200">名称</th>
                    <th width="40">单价</th>
                </tr>
                </thead>
                <tbody id="yigougoods">

                </tbody>
            </table>
        </div>
    </form>
</div>
<!--
<script id="demo2" type="text/html">
  <option value="">请选择</option>
  {{#  layui.each(d.list, function(index, item){ }}

  <option {{d.goodid== item.id?"selected":""}} value="{{ item.id }}">{{ item.goodName }}</option>

  {{#  }); }}
  {{#  if(d.list.length === 0){ }}
  <option disabled value="">无</option>
  {{#  } }}
</script>
-->
<script id="users" type="text/html">
    {{#  layui.each(d.list, function(index, item){ }}
    <option value="{{item.id}}">{{item.name}}</option>
    {{#  }); }}
</script>
<script id="goods2" type="text/html">
    {{#  layui.each(d.list, function(index, item){ }}
    <input type="checkbox" lay-filter="serviceadd" name="addservice" data-value="{{item.pice}}" value="{{item.id}}" title="{{item.serviceName}}">
    {{#  }); }}
</script>

<%include("/common/foot.html"){}%>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
<script src="js/formSelects-v4.js" type="text/javascript" charset="utf-8"></script>
<script>

    layui.use(['index', 'table', 'form', 'laydate', 'layedit', 'AjaxCommon', 'AliangLayui'], function () {
        var $ = layui.$;
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var upload = layui.upload;
        var layedit = layui.layedit;
        var AliangLayui = layui.AliangLayui;
        var index;


        //富文本编辑器
        layedit = layui.layedit;

        var title = "驳回意见";
        var size = ['100%', '100%']
        var titles = "订单信息";
        var sizes = ['100%', '100%']
        var findAll_url = 'purchaseserviceorder/list/findallorder.jhtml';
        var update_url = 'purchaseserviceorder/list/update.jhtml';
        var delete_url = 'purchaseserviceorder/list/delete.jhtml';

        var cols = [ //表头
            {field: 'orderNo', title: '订单编号'},
            {field: 'totalPrice', title: '价格'},
            {field: 'buyName', title: '购买用户', templet: "<div>{{d.sysUser.name}}</div>"},
            {field: 'phone', title: '用户联系方式', templet: "<div>{{d.sysUser.mobile}}</div>"},
            {field: 'accountname', title: '业务员'},
            // {field: 'goodname', title: '购买商品', toolbar: '#table-view-a'},
            {field: 'buyTime', title: '购买时间'},
            {title: '订单状态', toolbar: '#table-useradmin-webuser'},
            {title: '操作', toolbar: '#table-useradmin-opt', fixed: 'right'}
        ];
        AliangLayui.createTable(
                "#LAY-user-manage",
                "listReload",
                "#toolbarDemo",
                title,
                findAll_url,
                true,
                [10, 15, 20, 25],
                ['filter', 'print', 'exports'],
                cols,
                null
        );
        //自定义验证规则
        form.verify({
            required: [/[\S]+/, "必填项不能为空"],
            image: function (value) {
                if (value == "") {
                    return '您还未上传图片信息';
                }
            }

        });
        var totalArray = new Array();
        //弹出层
        //查看业务员
        function initUpdateBelongTo(data) {
            layer.open({
                title: "商品详细信息",
                area: size,
                type: 1,
                content: $('#table-huyang'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                btn: ['关闭'], //可以无限个按钮
                cancel: function (index, layero) {//取消事件
                    location.replace(location.href);　//layer.open关闭刷新
                },
            });


            layui.form.val("component-form-element", loadData(data));

        }

        form.on('checkbox(serviceadd)', function (data) {
            console.log($(data.elem).data("value"))

            if (data.elem.checked) {
                $("#totalPrice").val(Number($("#totalPrice").val()) + Number($(data.elem).data("value")));
            } else {
                $("#totalPrice").val(Number($("#totalPrice").val()) - Number($(data.elem).data("value")));
            }
        })
        form.on('select(goodId)', function (data1) {
            //alert(JSON.stringify(data1))
            // alert(1)
            $.ajax({
                url: "purchaseserviceorder/list/findgood.jhtml",
                type: "POST",
                data: {
                    gid: data1.value
                },
                dataType: "json",
                success: function (ret) {
                    // alert(JSON.stringify(ret))
                    //  alert(ret.obj.price)
                    $("#totalPrice").val(ret.obj.price)
                    form.render();
                },
                error: function (jqXHR, textStatus, errorThrown) {
                }
            });
        });
        form.on('checkbox(goodId)', function (data) {
            var pi = $("#totalPrice").val();

            if (data.elem.checked) {
                $("#totalPrice").val(Number(pi) + Number($(data.elem).parent().next().next().text()));
            } else {
                $("#totalPrice").val(Number(pi) - Number($(data.elem).parent().next().next().text()));
            }

            console.log($(data.elem).parent().next().next().text()); //得到checkbox原始DOM对象
            console.log(data.elem.checked); //是否被选中，true或者false
            console.log(data.value); //复选框value值，也可以通过data.elem.value得到
            console.log(data.othis); //得到美化后的DOM对象
        });

        //初始化修改页面
        function loadData(data) {
            var ret = {
                "id": data.id,
                "business": data.business,
                "goodsName": data.goodsName,
                "money": data.money,
                "brand": data.brand,
                "price": data.price,
                "company": data.company,
                "goodscontent": data.goodscontent
            }
            return ret;
        }

        //时间范围
        laydate.render({
            elem: '#test9'
            , type: 'time'
            , range: true
        });

        //建立编辑器
        index = layedit.build('content', {

            uploadImage: {url: 'system/goods/Img.jhtml', type: 'post'},
            height: 320
        });


        //初始化日期模块
        laydate.render({
            elem: '#create' //指定元素
        });
        //////////////////////////////////////以上是修改每个页面修改的地方
        //弹出修改页面调用初始化数据方法
        function initUpdate(data) {
            layer.open({
                title: title,
                area: size,
                type: 1,
                content: $('#layuiadmin-form-useradmin'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                btn: ['关闭'], //可以无限个按钮
                cancel: function (index, layero) {//取消事件
                    location.replace(location.href);　//layer.open关闭刷新
                },
            });


            layui.form.val("component-form-element", loadData(data));

        }


        function initUpdate(data) {
            layer.open({
                title: title,
                area: size,
                type: 1,
                content: $('#layuiadmin-form-useradmin'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                btn: ['关闭'], //可以无限个按钮
                cancel: function (index, layero) {//取消事件
                    location.replace(location.href);　//layer.open关闭刷新
                },
            });


            layui.form.val("component-form-element", loadData(data));

        }


        var sizeNew = ['900px', '700px'];

        function initUpdates(data) {
            layer.open({
                title: titles,
                area: ['100%', '100%'],
                type: 1,
                content: $('#layuiadmin-form-useradmins'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                btn: ['关闭'], //可以无限个按钮
                cancel: function (index, layero) {//取消事件
                    location.replace(location.href);　//layer.open关闭刷新
                },
            });
            // alert(1)
            $.ajax({
                url: "purchaseserviceorder/list/find.jhtml",
                type: "POST",
                data: {},
                dataType: "json",
                success: function (ret) {
                    //alert(JSON.stringify(ret))
                    var laytpl = layui.laytpl;
                    var ret2 = { //数据
                        "list": ret.obj
                    };
                    laytpl($("#goods2").html()).render(ret2, function (html) {
                        $("#listlist").html(html);
                    });
                    form.render();
                },
                error: function (jqXHR, textStatus, errorThrown) {
                }
            });
            $.ajax({
                url: "purchaseserviceorder/list/finduser.jhtml",
                type: "POST",
                data: {},
                dataType: "json",
                success: function (ret) {
                    //alert(JSON.stringify(ret))
                    var laytpl = layui.laytpl;
                    var ret2 = { //数据
                        "list": ret.obj
                    };
                    laytpl($("#users").html()).render(ret2, function (html) {
                        $("#userid").html(html);
                    });
                    form.render();
                },
                error: function (jqXHR, textStatus, errorThrown) {
                }
            });


            // layui.form.val("component-form-elements", loadDatas(data));

        }


        //修改页面的内容提交到后台处理
        form.on('submit(component-form-element)', function (data) {
            // alert(1)
            //data.field.content = layedit.getContent(index);
            postUpdate(data);
            return false;
        });
        //头工具栏事件
        table.on('toolbar(LAY-user-manage)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'add':
                    initUpdates(0);
                    break;
                case 'combination':
                    initUpdates(0);
                    break;
                case 'export':
                    layer.msg("导出");
                    break;
            }
        });

        //点击每行右工具栏“操作”里面按钮效果
        table.on('tool(LAY-user-manage)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                layer.msg('ID：' + data.id + ' 的查看操作');
            } else if (obj.event === 'hutan') {
                initUpdateBelongTo(data)
            } else if (obj.event === 'edit') {

                layer.msg("审核通过")


            } else if (obj.event === 'del') {
                initUpdate(data);
                // layer.confirm('删除以后数据将不能恢复，确定删除吗？', function (index) {
                //     //alert(data.id);
                //     postDelete(data.id);
                // });
            } else if (obj.event === 'chexiao') {
                //询问框
                layer.confirm('敏感操作，你是否需要撤销订单？', {
                    btn: ['确认', '取消'] //按钮
                }, function () {
                    //alert(JSON.stringify(obj))
                    $.ajax({
                        url: delete_url,
                        type: "POST",
                        data: {
                            "id": obj.data.id
                        },
                        dataType: "json",
                        success: function (ret) {
                            if (ret == null) {
                                layer.msg("撤销失败", {
                                    icon: 5
                                });
                            } else {
                                layer.msg("撤销成功", {
                                    icon: 6
                                });
                                layer.closeAll();
                                table.reload('listReload');
                            }
                            return ret;
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            var estr = "Page:" + window.location.pathname + ",Function: postDelete()";
                            layer.open({
                                title: "错误信息",
                                area: ['100%', '100%'],
                                type: 2,
                                content: "base/error.jhtml?info=" + estr, //
                                btn: ['关闭'] //可以无限个按钮
                            });
                        }
                    });
                }, function () {
                    /* layer.msg('也可以这样', {
                       time: 20000, //20s后自动关闭
                       btn: ['明白了', '知道了']
                     });*/
                });

            } else if (obj.event === 'resbon') {
                layer.open({
                    type: 1,
                    shade: false,
                    title: false, //不显示标题
                    content: obj.data.reason1, //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
                    cancel: function () {
                        //layer.msg('捕获就是从页面已经存在的元素上，包裹layer的结构', {time: 5000, icon:6});
                    }
                });

            }
        });


        //监听搜索按钮效果
        form.on('submit(LAY-user-front-search)', function (data) {

            var field = data.field;
            // alert(JSON.stringify(field))
            table.reload('listReload', {
                where: {
                    status: field.status,
                },
                page: {
                    curr: 1
                }
            });
        });
        //ajax提交修改内容
        window.postUpdate = function (data) {
            var formData = new FormData();
            formData.append("data", JSON.stringify(data.field));
            var arr = new Array();
            $("input:checkbox[name='addservice']:checked").each(function (i) {
                arr[i] = $(this).val();
                //alert()
            });
            formData.append("goods", arr);
            $.ajax({
                url: update_url,
                type: "POST",
                //traditional:true,
                data: formData,
                processData: false,
                contentType: false,
                success: function (data) {
                    if (data.code == 0) {
                        layer.closeAll();
                        layer.msg(data.msg, {time: 600, icon: 1}, function () {
                            //table.reload('listReload');
                            location.replace(location.href);
                        });
                    } else {
                        layer.msg(data.msg, {icon: 5});
                    }
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var estr = "Page:" + window.location.pathname + ",Function:postUpdate()";
                    layer.open({
                        title: "错误信息",
                        type: 2,
                        content: "base/error.jhtml?info=" + estr,
                        btn: ['关闭'] //可以无限个按钮
                    });
                }
            });
        }
        //ajax提交删除内容
        window.postDelete = function (id) {
            $.ajax({
                url: delete_url,
                type: "POST",
                data: {
                    "id": id
                },
                dataType: "json",
                success: function (ret) {
                    if (ret == null) {
                        layer.msg("删除失败", {
                            icon: 5
                        });
                    } else {
                        layer.msg("删除成功", {
                            icon: 6
                        });
                        layer.closeAll();
                        table.reload('listReload');
                    }
                    return ret;
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    var estr = "Page:" + window.location.pathname + ",Function: postDelete()";
                    layer.open({
                        title: "错误信息",
                        area: ['100%', '100%'],
                        type: 2,
                        content: "base/error.jhtml?info=" + estr, //
                        btn: ['关闭'] //可以无限个按钮
                    });
                }
            });
        }


    });

</script>
</body>

</html>